package com.hankol.community.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.baselibrary.utils.GlideUtils;
import com.baselibrary.view.CircleImageView;
import com.hankol.community.R;

import java.util.Collections;
import java.util.List;

/**
 * @author:hanke 2022/4/6
 * desc:
 */
public class AvatarsView extends LinearLayout {
    private View mRootView;
    private CircleImageView ciAvatarOne;
    private CircleImageView ciAvatarTwo;
    private CircleImageView ciAvatarThree, civAvatarFour, civAvatarFive;

    public AvatarsView(Context context) {
        super(context);
        init(context);
    }

    public AvatarsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AvatarsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public AvatarsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        mRootView = LayoutInflater.from(context).inflate(R.layout.layout_avatars_view, this);
        ciAvatarOne = mRootView.findViewById(R.id.civ_avatar_one);
        ciAvatarTwo = mRootView.findViewById(R.id.civ_avatar_two);
        ciAvatarThree = mRootView.findViewById(R.id.civ_avatar_three);
        civAvatarFour = mRootView.findViewById(R.id.civ_avatar_four);
        civAvatarFive = mRootView.findViewById(R.id.civ_avatar_five);
    }

    /**
     * @param order true = 从左到右回执    false 则相反...
     */
    public void setData(Context context, List<String> urls, boolean order) {
        for (String url : urls) {
            Log.d("======", "setData: "+url);
        }
        if (!order && urls.size() > 1) {
            Collections.reverse(urls);
        }
        if (urls.size() == 0) {
            ciAvatarOne.setVisibility(GONE);
            ciAvatarTwo.setVisibility(GONE);
            ciAvatarThree.setVisibility(GONE);
        } else if (urls.size() == 1) {
            GlideUtils.loadImg_avatar(context, urls.get(0), ciAvatarOne);
            ciAvatarOne.setVisibility(VISIBLE);
            ciAvatarTwo.setVisibility(GONE);
            ciAvatarThree.setVisibility(GONE);
        } else if (urls.size() == 2) {
            GlideUtils.loadImg_avatar(context, urls.get(0), ciAvatarOne);
            GlideUtils.loadImg_avatar(context, urls.get(1), ciAvatarTwo);
            ciAvatarOne.setVisibility(VISIBLE);
            ciAvatarTwo.setVisibility(VISIBLE);
            ciAvatarThree.setVisibility(GONE);
        } else if (urls.size() == 3) {
            GlideUtils.loadImg_avatar(context, urls.get(0), ciAvatarOne);
            GlideUtils.loadImg_avatar(context, urls.get(1), ciAvatarTwo);
            GlideUtils.loadImg_avatar(context, urls.get(2), ciAvatarThree);
            ciAvatarOne.setVisibility(VISIBLE);
            ciAvatarTwo.setVisibility(VISIBLE);
            ciAvatarThree.setVisibility(VISIBLE);
        } else if (urls.size() == 4) {
            ciAvatarOne.setVisibility(VISIBLE);
            ciAvatarTwo.setVisibility(VISIBLE);
            ciAvatarThree.setVisibility(VISIBLE);
            civAvatarFour.setVisibility(VISIBLE);
            GlideUtils.loadImg_avatar(context, urls.get(0), ciAvatarOne);
            GlideUtils.loadImg_avatar(context, urls.get(1), ciAvatarTwo);
            GlideUtils.loadImg_avatar(context, urls.get(2), ciAvatarThree);
            GlideUtils.loadImg_avatar(context, urls.get(3), civAvatarFour);
        } else if (urls.size() == 5) {
            ciAvatarOne.setVisibility(VISIBLE);
            ciAvatarTwo.setVisibility(VISIBLE);
            ciAvatarThree.setVisibility(VISIBLE);
            civAvatarFour.setVisibility(VISIBLE);
            civAvatarFive.setVisibility(VISIBLE);
            GlideUtils.loadImg_avatar(context, urls.get(0), ciAvatarOne);
            GlideUtils.loadImg_avatar(context, urls.get(1), ciAvatarTwo);
            GlideUtils.loadImg_avatar(context, urls.get(2), ciAvatarThree);
            GlideUtils.loadImg_avatar(context, urls.get(3), civAvatarFour);
            GlideUtils.loadImg_avatar(context, urls.get(4), civAvatarFive);
        }
    }
}
